//
//  NewHomeHeaderView.m
//  GC_OC
//
//  Created by  Quan He on 2022/4/8.
//

#import "NewHomeHeaderView.h"
#import "HomeMessageViewController.h"
#import "LearningHistoryViewController.h"
#import "SearchViewController.h"
#import "SearchVC.h"
#import "MineViewController.h"
#import "LoginViewController.h"
#import "FKLoginTool.h"

@interface NewHomeHeaderView()

@end

@implementation NewHomeHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatHeadView];
    }
    return self;
}

- (void)creatHeadView
{
    UIButton *messageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [messageBtn setImage:[UIImage imageNamed:@"xiaoxi"] forState:UIControlStateNormal];
    [messageBtn setTitle:@" 消息" forState:UIControlStateNormal];
    [messageBtn setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHex:@"#2BA3FF"]] forState:UIControlStateNormal];
    messageBtn.layer.cornerRadius = 12;
    messageBtn.layer.masksToBounds = YES;
    [messageBtn addTarget:self action:@selector(leftBtnAction) forControlEvents:UIControlEventTouchUpInside];
    messageBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [self addSubview:messageBtn];
    [messageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.equalTo(@(15));
        make.height.equalTo(@(24));
        make.width.equalTo(@(63));
    }];
    
    // 头像
    UIButton *rightbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightbtn setImage:[UIImage imageNamed:@"圣经_0"] forState:UIControlStateNormal];
    [rightbtn addTarget:self action:@selector(rightBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:rightbtn];
    [rightbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(@(-20));
        make.height.equalTo(@(24));
        make.width.equalTo(@(31));
        
    }];
    
    UIButton *historyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [historyBtn setTitle:@" 历史" forState:UIControlStateNormal];
    [historyBtn setImage:[UIImage imageNamed:@"lishi"] forState:UIControlStateNormal];
    [historyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    historyBtn.titleLabel.font=[UIFont systemFontOfSize:12 weight:UIFontWeightSemibold];
    historyBtn.layer.masksToBounds = YES;
    historyBtn.layer.cornerRadius = 8;
    [historyBtn setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHex:@"#2BA3FF"]] forState:UIControlStateNormal];
    
    historyBtn.layer.cornerRadius = 12;
    historyBtn.layer.masksToBounds = YES;
    [historyBtn addTarget:self action:@selector(historyBtnAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:historyBtn];
    [historyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(rightbtn.mas_left).offset(-8);
        make.height.equalTo(@(24));
        make.width.equalTo(@(64));
    }];

    UIView *searchView = [[UIView alloc] init];
    searchView.backgroundColor = RBG(240,240,240);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(searchAction)];
    [searchView addGestureRecognizer:tap];
    searchView.userInteractionEnabled = YES;
    searchView.layer.cornerRadius = 18;
    [self addSubview:searchView];
    [searchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(historyBtn.mas_left).offset(-8);
        make.left.equalTo(messageBtn.mas_right).offset(8);
        make.height.equalTo(@(36));
    }];
    
    [searchView addSubview:self.searchLabel];
}

- (void)searchAction {
    SearchVC* search=[[SearchVC alloc]init];
    search.hidesBottomBarWhenPushed=YES;
    search.searchID = self.searchID;
    [[KFTools getCurrentVC].navigationController pushViewController:search animated:YES];
    
//    SearchViewController* search = [[SearchViewController alloc]init];
//    search.hidesBottomBarWhenPushed=YES;
//    [[UIViewController currentViewController].navigationController pushViewController:search animated:YES];
}

- (void)leftBtnAction {
    self.messageAction();
}

- (void)historyBtnAction
{
    LearningHistoryViewController* learn=[[LearningHistoryViewController alloc]init];
    learn.hidesBottomBarWhenPushed=YES;
    [[UIViewController currentViewController].navigationController pushViewController:learn animated:YES];
}

- (void)rightBtnAction
{
    [FKLoginTool checkLogin:^{
        MineViewController* mine=[[MineViewController alloc]init];
        mine.hidesBottomBarWhenPushed=YES;
        [[UIViewController currentViewController].navigationController pushViewController:mine animated:YES];
    }];
}

- (SHInfiniteShufflingLabel *)searchLabel {
    if (!_searchLabel) {
        SHInfiniteShufflingLabel *label = [[SHInfiniteShufflingLabel alloc] initWithFrame:CGRectMake(16,2,SCREEN_WIDTH - 100,32)];
            [label setTextArray:@[@"搜索其实很简单"] InteralTime:4.0 Direction:SHRollingDirectionUp];
        _searchLabel = label;
        _searchLabel.userInteractionEnabled = NO;
    }
    return _searchLabel;
}

@end
